import re
import sys
import argparse

def get_file() :
    parser = argparse.ArgumentParser()
    parser.add_argument('--filename' , type=str , help='the file path you want to compress')
    args = parser.parse_args()
    return args

def compress_code(code : str) -> str :
    lines = code.split('\n')
    compressed_lines = []
    
    for line in lines :
        line = line.strip() 
        line = re.sub(r'\s*([=+\-*/(,)])\s*' ,r'\1' , line)
        compressed_lines.append(line)
    
    compressed_code = ' '.join(compressed_lines)
    return compressed_code
    

def compress_file(filename:str) -> str :
    with open(filename , 'r' , encoding='utf-8') as fn :
        code = fn.read() 
    return compress_code(code)

def flow_2_pool(compressed_code:str) -> str :
    compressed_pool = r'H:\work\code\ai_snake\pool.txt' 
    with open(compressed_pool ,'w' , encoding='utf-8') as p :
        p.write(compressed_code)


if __name__ == '__main__' :
    filename = get_file().filename
    if filename == '':
        print("Usage: python compress_code.py <file_name>") 
        sys.exit(1) 
    
    # print(compress_file(file_name))
    flow_2_pool(compress_file(filename))
